// <copyright file="BlogDataContext.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Blog.Data
{

    using System.Data.Entity;
    using Blog.Data.Entities;

    public class BlogDataContext : DbContext
    {
        public BlogDataContext() : base("DataContext") { }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
 
            #region Blog

            modelBuilder.Entity<BlogEntry>().ToTable("BlogEntry", "blg");
            modelBuilder.Entity<BlogEntryCategory>().ToTable("BlogEntryCategory", "blg");
            modelBuilder.Entity<BlogEntryComment>().ToTable("BlogEntryComment", "blg");
            modelBuilder.Entity<BlogEntryTag>().ToTable("BlogEntryTag", "blg");
            modelBuilder.Entity<BlogEntryRating>().ToTable("BlogEntryRating", "blg");

            modelBuilder.Entity<BlogEntryTag>()
                .HasMany(x => x.BlogEntries)
                .WithMany(x => x.BlogEntryTags)
            .Map(x =>
            {
                x.ToTable("BlogEntryTags", "blg");
                x.MapRightKey("Id");
                x.MapLeftKey("TagId");
            });

            #endregion

            #region Feed

            modelBuilder.Entity<Feed>().ToTable("Feeds", "feed");
            modelBuilder.Entity<FeedResults>().ToTable("FeedResults", "feed");
            modelBuilder.Entity<FeedXPath>().ToTable("FeedFetchRules", "feed");
            modelBuilder.Entity<XPathRemove>().ToTable("FeedCleanRules", "feed");

            #endregion

        }

        public DbSet<BlogEntry>             BlogEntries { get; set; }
        public DbSet<BlogEntryTag>          BlogEntryTags { get; set; }
        public DbSet<BlogEntryCategory>     BlogEntryCategories { get; set; }
        public DbSet<BlogEntryComment>      BlogEntryComments { get; set; }
        public DbSet<BlogEntryRating>       BlogEntryRatings { get; set; }

        public DbSet<Feed>                  Feeds { get; set; }
        public DbSet<FeedResults>           FeedResults { get; set; }
        public DbSet<FeedXPath>             FeedFetchRules { get; set; }
        public DbSet<XPathRemove>           FeedCleanRules { get; set; }

    }
}